﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using myFinance.Controls;

namespace myFinance
{
    public partial class MainForm : Form
    {
        protected enum ScreenControl
        {
            AddExpence,
            AddFromDefinedExpence,
            AddIncome,
            AllExpences,
            Summary
        }

        protected ScreenControl DisplayingScreenControl;
        protected Control prevControl;
        protected DAL.IDAL DALProvider;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.DALProvider = DAL.DALFactory.GetDAL<DAL.SqlCeDAL>();

            this.prevControl = new SummaryScreen();
            this.prevControl.Top = 0;
            this.prevControl.Left = 0;
            this.Controls.Add(this.prevControl);
        }

        private void miAddExpence_Click(object sender, EventArgs e)
        {
            ClearScreen();
            AddControl(new AddExpence());
            this.DisplayingScreenControl = ScreenControl.AddExpence;
        }

        private void miAddDefExpence_Click(object sender, EventArgs e)
        {
            ClearScreen();
            AddControl(new AddFromDefinedExpence());
            this.DisplayingScreenControl = ScreenControl.AddFromDefinedExpence;
        }

        private void miAddIncome_Click(object sender, EventArgs e)
        {
            ClearScreen();
            AddControl(new AddIncome());
            this.DisplayingScreenControl = ScreenControl.AddIncome;
        }

        private void miAllExpences_Click(object sender, EventArgs e)
        {
            ClearScreen();
            AddControl(new AllExpences());
            this.DisplayingScreenControl = ScreenControl.AllExpences;
        }

        private void miSummary_Click(object sender, EventArgs e)
        {
            ClearScreen();
            AddControl(new SummaryScreen());
            this.DisplayingScreenControl = ScreenControl.Summary;
        }

        private void miOK_Click(object sender, EventArgs e)
        {
            switch (this.DisplayingScreenControl)
            {
                case ScreenControl.AddExpence:
                    var expence = ((Entity.IExpence)this.Controls[0]);
                    this.DALProvider.SaveExpence(expence);
                    miAllExpences_Click(this, null);
                    break;

                case ScreenControl.AddFromDefinedExpence:
                    var defExpence = (Entity.IExpence)((AddFromDefinedExpence)this.Controls[0])
                        .GetExpence();
                    this.DALProvider.SaveExpence(defExpence);
                    miAllExpences_Click(this, null);
                    break;

                case ScreenControl.AddIncome:
                    var income = (Entity.IIncome)(this.Controls[0]);
                    this.DALProvider.SaveIncome(income);
                    break;

                case ScreenControl.AllExpences:
                    miSummary_Click(this, null);
                    break;

                case ScreenControl.Summary:
                    this.DisplayingScreenControl = ScreenControl.Summary;
                    break;
            }
        }

        protected void AddControl(Control control)
        {
            this.prevControl = control;
            control.Top = 0;
            control.Left = 0;
            this.Controls.Add(control);
        }

        protected void ClearScreen()
        {
            this.Controls.Remove(this.prevControl);
            this.prevControl.Dispose();
        }
    }
}